package com.medvalley.tcm.service.impl;

import com.google.common.collect.Lists;
import com.medvalley.tcm.base.BusinessException;
import com.medvalley.tcm.dao.MusicDocCategoryDao;
import com.medvalley.tcm.dao.MusicDocDao;
import com.medvalley.tcm.domain.MusicDoc;
import com.medvalley.tcm.domain.MusicDocCategory;
import com.medvalley.tcm.vo.outputvo.MusicDocCategoryOutputVo;
import com.medvalley.tcm.vo.outputvo.MusicDocOutputVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class MusicDocService {
    @Autowired
    private MusicDocDao musicDocDao;

    @Autowired
    private MusicDocCategoryDao musicDocCategoryDao;

    public List<MusicDocCategoryOutputVo> listCategory(int categoryId) {
        List<MusicDocCategoryOutputVo> musicDocCategoryOutputVoList = Lists.newArrayList();
        List<MusicDocCategory> musicDocCategoryList = musicDocCategoryDao.findAllByParentId(categoryId);
        if (!CollectionUtils.isEmpty(musicDocCategoryList)) {
            musicDocCategoryList.forEach(c -> {
                MusicDocCategoryOutputVo musicDocCategoryOutputVo = new MusicDocCategoryOutputVo();
                BeanUtils.copyProperties(c, musicDocCategoryOutputVo);

                List<MusicDocCategoryOutputVo> subList = Lists.newArrayList();
                List<MusicDocCategory> subMusicDocCategoryList = musicDocCategoryDao.findAllByParentId(c.getId());
                if (!CollectionUtils.isEmpty(subMusicDocCategoryList)) {
                    subMusicDocCategoryList.forEach(sm -> {
                        MusicDocCategoryOutputVo subOutputVo = new MusicDocCategoryOutputVo();
                        BeanUtils.copyProperties(sm, subOutputVo);
                        subList.add(subOutputVo);
                    });
                }
                musicDocCategoryOutputVo.setSubList(subList);
                musicDocCategoryOutputVoList.add(musicDocCategoryOutputVo);
            });
        }
        return musicDocCategoryOutputVoList;
    }

    public List<MusicDocOutputVo> listMusicDoc(int categoryId) {
        List<MusicDocOutputVo> musicDocOutputVoList = Lists.newArrayList();
        List<MusicDoc> musicDocList = musicDocDao.findAllByDocCategory(categoryId);
        if (!CollectionUtils.isEmpty(musicDocList)) {
            musicDocList.forEach(m -> {
                MusicDocOutputVo musicDocOutputVo = new MusicDocOutputVo();
                BeanUtils.copyProperties(m, musicDocOutputVo);
                musicDocOutputVoList.add(musicDocOutputVo);
            });
        }
        return musicDocOutputVoList;
    }

    public MusicDocOutputVo docDetail(int docId) {
        MusicDocOutputVo musicDocOutputVo = new MusicDocOutputVo();
        MusicDoc musicDoc = musicDocDao.findById(docId).orElseThrow(() -> new BusinessException("无此文章"));
        BeanUtils.copyProperties(musicDoc, musicDocOutputVo);
        return musicDocOutputVo;
    }

    public void addDoc(MusicDoc musicDoc) {
        musicDocDao.save(musicDoc);
    }
}
